package com.itestor.iptv.global.actions;

import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.itestor.iptv.global.bean.OFCGraph;

/**
 * 抽象的Open Flash Chart数据Action.
 * @author TrueOrFalse.Yuan
 */
public abstract class AbstractOFCDataAction extends Action {

	/**
	 * Logger for this class.
	 */
	private static final Logger logger = Logger.getLogger(AbstractOFCDataAction.class);

	protected OFCGraph graph = new OFCGraph();

	public AbstractOFCDataAction(OFCGraph g) {
		if (g != null) {
			this.graph = g;
		}
	}

	public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		response.setHeader("Content-Type", "text/plain");
		response.setHeader("Cache-Control", "no-cache");
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();

		OFCGraph graphData = buildData(form, request);
		if (graphData != null) {
			String s = graphData.render();
			logger.info("OFC Data[" + request.getRequestURI() + "]" + s);
			out.write(s);
		}
		out.close();
		return null;
	}

	public abstract OFCGraph buildData(ActionForm form, HttpServletRequest request);
}
